package com.example.popmp3.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.popmp3.R;
import com.example.popmp3.dao.MusicListDAO;
import com.example.popmp3.model.Tb_musiclist;
import com.example.popmp3.model.Tb_singlelist;

public class MainActivity extends Activity implements OnClickListener {
    ListView listView1;// 创建ListView对象
    EditText etSearch;
    Button btsList,btlList,btSearch;
    String idType = "";// 创建字符串，记录管理类型
    String mName;
    String strInfo;
    String[] strInfos = null;// 定义字符串数组
    ArrayAdapter<String> arrayAdapter = null;// 创建ArrayAdapter对象
    MusicListDAO musiclistinfo = new MusicListDAO(MainActivity.this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btsList = (Button) findViewById(R.id.btsList);
        btlList = (Button) findViewById(R.id.btlList);
        btSearch = (Button) findViewById(R.id.btSearch);
        etSearch = (EditText) findViewById(R.id.etSearch);
       final String songName = etSearch.getText().toString();
        listView1 = (ListView) findViewById(R.id.listView1); // 获取布局管理器中添加的ListView组件

        ShowInfo(R.id.btsList);
        btsList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowInfo(R.id.btsList);
            }
        });
        btlList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoveList.class);
                startActivity(intent);
            }
        });
        listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
                String strid = strInfo.substring(0, strInfo.indexOf(' '));// 从项信息中截取编号
                idType="0";
                Intent intent = new Intent(MainActivity.this, PlayMusic.class);// 使用InfoManage窗口初始化Intent对象
                intent.putExtra("id",new String[]{strid,idType});
                startActivity(intent);// 执行Intent，打开相应的Activity
            }
        });
        listView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {

                AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
                alert.setTitle("Warning!");    //设置对话框的标题
                alert.setIcon(android.R.drawable.ic_dialog_info);
                alert.setMessage("Are You Sure To Delete?");
                String strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
                final String strid = strInfo.substring(0, strInfo.indexOf(' '));
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "您取消删除歌曲！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                //添加确定按钮
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        musiclistinfo.detele(Integer.parseInt(strid));
                        Toast.makeText(MainActivity.this, "删除歌曲成功！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show(); // 创建对话框并显示
                return true;
            }
        });
        btSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Tb_musiclist> tb_musiclists = musiclistinfo.getScrollData(0,
                        (int) musiclistinfo.getCount());
               strInfos = new String[tb_musiclists.size()];
                int m = 0;// 定义一个开始标识
                for (Tb_musiclist tb_musiclist : tb_musiclists) {// 遍历List泛型集合
                if (musiclistinfo.find(m+1).toString().equals(songName)) {
                    strInfos[m] = tb_musiclist.getid() + " " + tb_musiclist.getName();
                    ShowInfo(m+1);
                }
                    m++;// 标识加1
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
    }
    public void ShowInfo(int id) {

        List<Tb_musiclist> tb_musiclists = musiclistinfo.getScrollData(0,
                (int) musiclistinfo.getCount());
        strInfos = new String[tb_musiclists.size()];// 设置字符串数组的长度
        int m = 0;// 定义一个开始标识
        for (Tb_musiclist tb_musiclist : tb_musiclists) {// 遍历List泛型集合
            strInfos[m] = tb_musiclist.getid() + " " + tb_musiclist.getName();
            m++;// 标识加1
        }
        // 使用字符串数组初始化ArrayAdapter对象
        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, strInfos);
        listView1.setAdapter(arrayAdapter);// 为ListView列表设置数据源
    }
    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();// 实现基类中的方法
        ShowInfo(R.id.btsList);
    }
}
